<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/5
 * Time: 9:44
 */

namespace app\admin\controller;
use app\Model\CommentModel;
use think\Controller;
use think\Db;

class Comment extends Common
{
    //商品评论首页
    //$list 评论表与商品表联合查询且用户名不等于admin的数据
    //neq ！=
   public function commentindex(){
       $postData = input('post.');
       $where = [];
       if (isset($postData['searchContent'])){
           if ($postData['searchContent']){//内容
               $where['content'] = ['LIKE', "%".$postData['searchContent']."%"];
           }
           if ($postData['searchUser']){//用户名
               $where['username'] = ['LIKE', "%".$postData['searchUser']."%"];
           }
       }
       $list=CommentModel::
           alias('c')
           ->where($where)
           ->where('parent_id','=',0)
           ->join('tp_goods g','c.goods_id=g.goods_id')
           ->order('add_time','desc')
           ->paginate(null,false,['query'=>request()->param()]);
//       dump($list);die();
       $this->assign('list',$list);
       $this->getConAndAct('Goods');
       return view('CommentIndex');
   }
    /*
    * 评论详情页
    * goods_id 商品id
    * $reply 查询管理员回复的消息
    */
     public function CommentDetail(){
        $getData=input('get.');
        $data['comment']=CommentModel::find($getData['comment_id']);//用户评论
         $data['comment']['img']=explode(',',$data['comment']['img']);
         $data['admin_comment']=CommentModel::where('parent_id','=',$getData['comment_id'])->select();

        return view('CommentDetail',$data);
   }
    /*
     * 回复评论
     */
    public function addData()
    {

        $getData=input('post.');
//        dump(session('admin.user_name'));
        $insert=[
            'username'=>session('admin.user_name'),
            'content'=>$getData['content'],
            'add_time'=>time(),
            'parent_id'=>$getData['content_id'],
        ];
        $addComment=CommentModel::insert($insert);

        //修改用户评论的回复状态
        CommentModel::update(['comment_id'=>$getData['content_id'],'reply'=>1]);
        return setDatabase($addComment,'回复');
    }
//删除
public function CommentDelete(){
    $id=input('id');
    $res=CommentModel::where('comment_id',$id)->delete();
    if ($res){
        return retJson('',200,'删除成功');
    }else{
        return retJson('',201,'删除失败');
    }
}

//评论搜索
public function CommentSearch(){
$searchContent=input('searchContent');
$searchUser=input('searchUser');
    $list=Db('tp_comment')
        ->alias('c')
        ->where('username','NEQ',session('admin')['user_name'])
        ->where('content','like',"%$searchContent%")
        ->whereOr('username','like',"%$searchUser%")
        ->join('tp_goods g','c.goods_id=g.goods_id')
        ->paginate(20,false,[
            'query' => request()->param(), ]);
    $this->assign('list',$list);
    return $this->fetch('CommentIndex');
}
}